<template>
  <el-form ref="basicInfoForm" :model="checkUp" :rules="rules" label-width="120px" label-position="left">
    <div class="app-container">
    <el-row>
      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span>数据源</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
           <el-row :gutter="50">
              <el-col :span="13" >
                <el-form-item label="url" prop="url">
                  <el-input placeholder="请输入数据库url" v-model="checkUp.url" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="13">
                <el-form-item label="用户名" prop="username">
                  <el-input placeholder="请输入数据库用户名" v-model="checkUp.username" clearable />
                </el-form-item>
              </el-col>

              <el-col :span="2">
                <el-button  type="primary" icon="el-icon-upload" @click="connect()">连接</el-button>
              </el-col>

              <el-col :span="13">
                <el-form-item label="密码" prop="password">
                  <el-input placeholder="请输入数据库密码" v-model="checkUp.password" show-password  clearable />
                </el-form-item>
              </el-col>

               <el-col :span="2">
                  <el-button type="primary" icon="el-icon-download" @click="submitForm()">获取</el-button>
              </el-col>
    
            </el-row>
          </div>
        </el-card>
      </el-col>

      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span>项目设置</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
             <el-row :gutter="50" >
              
              <el-col :span="12">
                <el-form-item label="作者" prop="author">
                  <el-input placeholder="作者 eg: zengpeng" v-model="checkUp.author" clearable />
                </el-form-item>
              </el-col>

               <el-col :span="12">
                <el-form-item label="模块名称" prop="moduleName">
                  <el-input placeholder="模块名称 eg: system" v-model="checkUp.moduleName" clearable />
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="项目名称" prop="projectName">
                  <el-input placeholder="项目名称 eg: fish" v-model="checkUp.projectName" clearable />
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="表名称" prop="targetTable">
                  <el-select v-model="checkUp.targetTable" multiple collapse-tags :disabled="tablesIsOk"  :placeholder="placeholderIsOk" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in tables" :key="index" :label="item.tableComment"
                      :value="item.tableName" :disabled="item.disabled">
                      <span style="float: left">{{ item.tableComment }}</span>
                      <span style="float: right; color: #8492a6; font-size: 13px;margin-right: 17px;">{{ item.tableName }}</span>
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="上级目录" prop="parent">
                  <el-input placeholder="上级目录 eg: com.zp.module" v-model="checkUp.parent" clearable />
                </el-form-item>
              </el-col>

               <el-col :span="12">
                <el-form-item label="主键类型" >
                  <el-select v-model="config.global.idType" placeholder="主键类型" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in idTypes" :key="index" :label="item.label"
                      :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>

       <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span>全局配置</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
           <el-row :gutter="50">
             <el-col :span="12">
                <el-form-item label="注入Lombok" >
                  <el-switch v-model="config.strategyInfo.entityLombokModel"></el-switch>
                </el-form-item>
              </el-col>
              
              <el-col :span="12">
                <el-form-item label="RestController" >
                  <el-switch v-model="config.strategyInfo.restController"></el-switch>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="注入swagger2" prop="swagger2">
                  <el-switch v-model="config.global.swagger2"></el-switch>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="BaseResultMap" prop="baseResultMap">
                  <el-switch v-model="config.global.baseResultMap"></el-switch>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="@TableField" >
                  <el-switch v-model="config.strategyInfo.tableField"></el-switch>
                </el-form-item>
              </el-col>
              
              <el-col :span="12">
                <el-form-item label="BaseResultList" prop="baseResultList">
                  <el-switch v-model="config.global.baseResultList"></el-switch>
                </el-form-item>
              </el-col>

              <!-- <el-col :span="12">
                <el-form-item label="注入二级缓存" prop="enableCache">
                 <el-switch v-model="config.global.enableCache"></el-switch>
                </el-form-item>
              </el-col> -->

           
            </el-row>
          </div>
        </el-card>
      </el-col>

      <el-col :span="12" class="card-box">
        <el-card>
          <div slot="header"><span>模板选择</span></div>
          <div class="el-table el-table--enable-row-hover el-table--medium">
           <el-row :gutter="50">
              <el-col :span="12">
                <el-form-item label="controller模板" >
                  <el-select v-model="config.template.controller" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in controllers" :key="index" :label="item.label"
                      :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

               <el-col :span="12">
                <el-form-item label="service模板" >
                  <el-select v-model="config.template.service" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in services" :key="index" :label="item.label"
                      :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

               <el-col :span="12">
                <el-form-item label="serviceImpl模板" >
                  <el-select v-model="config.template.serviceImpl" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in serviceImpls" :key="index" :label="item.label"
                      :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

               <el-col :span="12">
                <el-form-item label="mapper模板" >
                  <el-select v-model="config.template.mapper" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in mappers" :key="index" :label="item.label"
                      :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

               <el-col :span="12">
                <el-form-item label="entity模板" >
                  <el-select v-model="config.template.entity" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
                    <el-option v-for="(item, index) in entitys" :key="index" :label="item.label"
                      :value="item.value" :disabled="item.disabled"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="是否支持驼峰" >
                  <el-switch v-model="config.strategyInfo.controllerMappingHyphenStyle"></el-switch>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>

    </el-row>
  </div>
  
  </el-form>
</template>


<script>
import { generate, listDbTable } from "@/api/tool/mybatis";
import { downLoadZip } from "@/utils/zipdownload";
export default {
  name: "BasicInfoForm",
  props: {
     info: {
        type: Object,
        default: null
      }
  },
  methods: {
    connect() {
      this.config.dataSource.url = this.checkUp.url;
      this.config.dataSource.username = this.checkUp.username;
      this.config.dataSource.password = this.checkUp.password;
      listDbTable(this.config.dataSource).then(res => {
        if (res.code === 200) {
          this.tables = res.data;
          this.tablesIsOk=false;
          this.placeholderIsOk="请选择表名";
        }
        this.msgSuccess('获取表名称成功');
      });
    },

     /** 提交按钮 */
    submitForm() {
      this.config.dataSource.url = this.checkUp.url;
      this.config.dataSource.username = this.checkUp.username;
      this.config.dataSource.password = this.checkUp.password;

      this.config.targetTable.projectName = this.checkUp.projectName;
      this.config.targetTable.moduleName = this.checkUp.moduleName;
      this.config.targetTable.targetTable = this.checkUp.targetTable;

      this.config.packageSet.parent = this.checkUp.parent;
      this.config.global.author = this.checkUp.author;

      console.log(this.config);
      this.$refs["basicInfoForm"].validate((valid) => {
        console.log(this.config);
        if (valid) {
          generate(this.config).then((response) => {
            if (response.code === 200) {
              downLoadZip("/generator/downLoadZip?projectName=" + this.config.targetTable.projectName
              +"&parent="+ this.config.packageSet.parent+"&moduleName="+this.config.targetTable.moduleName, "fish");
            }
          });
        }
      });
    }
  },
  
  data() {
    return {
      checkUp:{
        url:"jdbc:mysql://127.0.0.1:3306/confusion_fish?serverTimezone=GMT%2B8",
        username:"",
        password:"",
        author:"zengpeng",
        projectName:"fish",
        parent:"com.zp.module",
        moduleName:"system",
        targetTable:[],
      },
     
      config: {
        targetTable:{
          projectName:"fish",
          moduleName:"",
          targetTable:[]
        },

        dataSource:{
          url:"",
          username:"root",
          password:""
        },
      
        global:{
          author:"zengpeng",
          baseResultMap: true,
          baseResultList: true,
          swagger2: true,
          enableCache: false,
          idType:"AUTO"
        },
        packageSet:{
          parent:"com.zp.module",
        },
        strategyInfo:{
          restController: true,
          entityLombokModel: true,
          tableField: true,
          controllerMappingHyphenStyle: true
        },
        template:{
          controller:"templatesMy/controller.java",
          service:"templatesMy/service.java",
          serviceImpl:"templatesMy/serviceImpl.java",
          mapper:"templatesMy/mapper.java",
          entity:"templatesMy/entity.java",
        }
      },
      tablesIsOk:true,
      placeholderIsOk:"请先连接数据库",
      rules: {
        url: [
          { required: true, message: "请输入数据库url", trigger: "blur" }
        ],
        username: [
          { required: true, message: "请输入数据库用户名", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入数据库密码", trigger: "blur" }
        ],
        author: [
          { required: true, message: "请输入作者", trigger: "blur" }
        ],
        moduleName: [
          { required: true, message: "请输入模块名称", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "请输入项目名称", trigger: "blur" }
        ],
        targetTable: [
          { required: true, message: "请输入表名称", trigger: "blur" }
        ],
        parent: [
          { required: true, message: "请输入上级目录", trigger: "blur" }
        ],
        idType: [
          { required: true, message: "请输入主键类型", trigger: "blur" }
        ]
      },
      //主键类型
      idTypes: [{
        "label": "自动递增",
        "value": "AUTO"
      }, {
        "label": "跟随全局",
        "value": "NONE"
      },{
        "label": "用户输入",
        "value": "INPUT"
      }, {
        "label": "为空自动填充int",
        "value": "ASSIGN_ID"
      },{
        "label": "为空自动填充UUID",
        "value": "ASSIGN_UUID"
      }, {
        "label": "手动int",
        "value": "ID_WORKER"
      },{
        "label": "手动String",
        "value": "ID_WORKER_STR"
      }, {
        "label": "UUID",
        "value": "UUID"
      }],
      //模板下拉
      controllers:[{
        "label": "灯笼鱼",
        "value": "templatesMy/controller.java"
      }, {
        "label": "hjmos",
        "value": "templatesHjmos/controller.java"
      }],
      services:[{
        "label": "灯笼鱼",
        "value": "templatesMy/service.java"
      }, {
        "label": "hjmos",
        "value": "templatesHjmos/service.java"
      }],
      serviceImpls:[{
        "label": "灯笼鱼",
        "value": "templatesMy/serviceImpl.java"
      }, {
        "label": "hjmos",
        "value": "templatesHjmos/serviceImpl.java"
      }],
      mappers:[{
        "label": "灯笼鱼",
        "value": "templatesMy/mapper.java"
      }, {
        "label": "hjmos",
        "value": "templatesHjmos/mapper.java"
      }],
      entitys:[{
        "label": "灯笼鱼",
        "value": "templatesMy/entity.java"
      }, {
        "label": "hjmos",
        "value": "templatesHjmos/entity.java"
      }],
      //tables,数据库中的表
      tables:[{
        "tableComment": "用户表",
        "tableName": "sys_user"
      }, {
        "tableComment": "配置表",
        "tableName": "sys_config"
      }]
    };
  }
};
</script>
